﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace BlueLotus7.Common
{
    public class UploadFile : IValidate
    {
        #region Fields

        protected Guid key;
        protected Guid ownerKey;
        protected byte[] fileData;
        protected string fileName;
        protected string fileExtention;
        protected DateTime createdStamp;
        protected DateTime lastUpdatedStamp;
        protected string md5Code;
        protected string fileDescription;

        #endregion

        #region Property

        public Guid Key
        {
            get { return key; }
        }

        public Guid OwnerKey
        {
            get { return ownerKey; }
        }

        public byte[] FileData
        {
            get { return fileData; }
            set { fileData = value; }
        }

        public string FileName
        {
            get { return fileName; }
            set { fileName = value; }
        }

        public string FileExtention
        {
            get { return fileExtention; }
            set { fileExtention = value; }
        }

        public DateTime CreatedStamp
        {
            get
            {
                return createdStamp;
            }
        }

        public DateTime LastUpdatedStamp
        {
            get
            {
                return lastUpdatedStamp;
            }
            set
            {
                lastUpdatedStamp = value;
            }
        }

        public string FileDescription
        {
            get { return fileDescription; }
            set { fileDescription = value; }
        }

        public int? FileSize
        {
            get
            {
                int? size = null;
                if (fileData != null)
                {
                    size = fileData.Length;
                }
                return size;
            }
        }

        public string Md5Code
        {
            get { return md5Code; }
            set { md5Code = value; }
        }

        #endregion

        #region Constructor

        public UploadFile(Guid ownerKey, string fileName, string fileExtention, byte[] fileData)
        {
            this.key = Guid.NewGuid();
            this.createdStamp = DateTime.Now;
            this.lastUpdatedStamp = createdStamp;

            this.ownerKey = ownerKey;
            this.fileName = fileName;
            this.fileData = fileData;
            this.fileExtention = fileExtention;
        }

        public UploadFile(Guid ownerKey, string fullName, byte[] fileData)
            : this(ownerKey, null, null, fileData)
        {
            this.fileName = SplitFileNameAndExtention(fullName, out this.fileExtention);
        }

        public UploadFile(Guid key, Guid ownerKey, string fileName, string fileExtention, byte[] fileData, DateTime createdStamp)
        {
            this.key = key;
            this.createdStamp = createdStamp;

            this.ownerKey = ownerKey;
            this.fileName = fileName;
            this.fileData = fileData;
            this.fileExtention = fileExtention;
        }

        #endregion

        #region IValidate Members

        public bool IsValid
        {
            get
            {
                try
                {
                    Validate();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        public virtual void Validate()
        {
            if (Validator.IsZeroGuid(key))
            {
                throw new InvalidObjectException("key");
            }
            else if (string.IsNullOrWhiteSpace(fileName))
            {
                throw new NullOrEmptyStringException("fileName");
            }
            else if (string.IsNullOrWhiteSpace(fileExtention))
            {
                throw new NullOrEmptyStringException("fileExtention");
            }
            else if (Validator.IsZeroDateTime(createdStamp))
            {
                throw new NullReferenceException("createdStamp");
            }
            else if (Validator.IsZeroDateTime(lastUpdatedStamp))
            {
                throw new NullReferenceException("lastUpdatedStamp");
            }
            else if (fileData == null || fileData.Length == 0)
            {
                throw new InvalidObjectException("fileData");
            }
        }

        #endregion

        public void Export(string folderPath)
        {
            if (string.IsNullOrWhiteSpace(folderPath))
            {
                throw new NullOrEmptyStringException("folderPath");
            }

            if (!folderPath.EndsWith(@"\"))
            {
                folderPath += @"\";
            }

            File.WriteAllBytes(folderPath + fileName + "." + fileExtention, fileData);
        }

        public static string SplitFileNameAndExtention(string fullName, out string extentionName)
        {
            extentionName = null;
            if (!string.IsNullOrWhiteSpace(fullName))
            {
                int indexOfLastDot = fullName.LastIndexOf('.');
                if (indexOfLastDot > -1)
                {
                    extentionName = fullName.Substring(indexOfLastDot + 1);
                    fullName = fullName.Substring(0, indexOfLastDot);
                }
            }

            return fullName;
        }

    }
}
